package market

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/market/outbound_crowd"
	"git.myscrm.cn/golang/stark/v4"
)

type outboundCrowdServer struct {
	outboundCrowdService outbound_crowd.OutboundCrowdServiceIface
}

func NewOutboundCrowdServer(outboundCrowdService outbound_crowd.OutboundCrowdServiceIface,
) pb.OutboundCrowdServiceServer {
	return &outboundCrowdServer{
		outboundCrowdService: outboundCrowdService,
	}
}

// GetCrowdList 人群列表
func (s *outboundCrowdServer) GetCrowdList(ctx context.Context, request *pb.GetCrowdListRequest) (*pb.GetCrowdListResponse, error) {
	res, err := s.outboundCrowdService.GetCrowdList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdList err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCrowdDetail 人群详情
func (s *outboundCrowdServer) GetCrowdDetail(ctx context.Context, request *pb.GetCrowdDetailRequest) (*pb.GetCrowdDetailResponse, error) {
	var errMsg string
	if request.GetId() == 0 {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.outboundCrowdService.GetCrowdDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdDetail err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// AddCrowd 添加、保存人群
func (s *outboundCrowdServer) AddCrowd(ctx context.Context, request *pb.AddCrowdRequest) (*pb.AddCrowdResponse, error) {
	res, err := s.outboundCrowdService.AddCrowd(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "AddCrowd err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// DeleteCrowd 删除人群
func (s *outboundCrowdServer) DeleteCrowd(ctx context.Context, request *pb.DeleteCrowdRequest) (*pb.DeleteCrowdResponse, error) {
	var errMsg string
	if request.GetId() == 0 {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.outboundCrowdService.DeleteCrowd(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdDetail err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCrowdByWinbid 获取中标单位人群
func (s *outboundCrowdServer) GetCrowdByWinbid(ctx context.Context, request *pb.GetCrowdByWinbidRequest) (*pb.GetCrowdByWinbidResponse, error) {
	res, err := s.outboundCrowdService.GetCrowdByWinbid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByWinbid err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCrowdByDeveloper 获取甲方单位人群
func (s *outboundCrowdServer) GetCrowdByDeveloper(ctx context.Context, request *pb.GetCrowdByDeveloperRequest) (*pb.GetCrowdByDeveloperResponse, error) {
	res, err := s.outboundCrowdService.GetCrowdByDeveloper(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByDeveloper err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetCrowdByRpa 获取RPA人群
func (s *outboundCrowdServer) GetCrowdByRpa(ctx context.Context, request *pb.GetCrowdByRpaRequest) (*pb.GetCrowdByRpaResponse, error) {
	res, err := s.outboundCrowdService.GetCrowdByRpa(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetCrowdByRpa err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetRpaContactMobile 获取RPA联系人手机号
func (s *outboundCrowdServer) GetRpaContactMobile(ctx context.Context, request *pb.GetRpaContactMobileRequest) (*pb.GetRpaContactMobileResponse, error) {
	res, err := s.outboundCrowdService.GetRpaContactMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRpaContactMobile err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// 人群详情：中标人群数据
func (s *outboundCrowdServer) CrowdDetailWinbid(ctx context.Context, request *pb.CrowdDetailWinbidRequest) (*pb.CrowdDetailWinbidResponse, error) {
	var errMsg string
	if request.GetCrowdId() == 0 {
		errMsg = "crowd_id参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	res, err := s.outboundCrowdService.CrowdDetailWinbid(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailWinbid err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// 人群详情：甲方单位人群数据
func (s *outboundCrowdServer) CrowdDetailDeveloper(ctx context.Context, request *pb.CrowdDetailDeveloperRequest) (*pb.CrowdDetailDeveloperResponse, error) {
	var errMsg string
	if request.GetCrowdId() == 0 {
		errMsg = "crowd_id参数不能为空"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}

	res, err := s.outboundCrowdService.CrowdDetailDeveloper(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CrowdDetailDeveloper err：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
